package com.cloud.common.core.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    // 创建ObjectMapper对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    // 对象转为Json
    public static String objToJson(Object obj){
        try {
            return MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    // Json转对象
    public static <T> T  jsonToBean(String json,Class<T> beanType){
        try {
            return MAPPER.readValue(json,beanType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

